# -*- coding: utf-8 -*-
#error_code.py

import logging

class Error(Exception):
    code    =0
    msg =None
    note    =None
    level   =None
    name    =""
    
    def __init__(self,note="empty"):
        Exception.__init__(self)
        self.set_note(note)
        
    def set_note(self,note):
        self.note=note
    def get_note(self):
        return self.note
    
    def to_dict(self):
        dict={'code':self.code,'level':self.level,'name':self.name,'note':self.note,'msg':self.msg}
        return dict
        


    def __str__(self):
        return "    -    CODE:%d MSG:%s NOTE:%s \n"%(self.code,self.msg,self.note)
    
    def log(self):
        if(self.level   ==  logging.DEBUG):
            logging.debug(self.__str__())

        if(self.level   ==  logging.INFO):
            logging.info(self.__str__())
            
        if(self.level   ==  logging.WARNING):
            logging.warning(self.__str__())
        
        if(self.level   ==  logging.ERROR):
            logging.error(self.__str__())
        
        if(self.level   ==  logging.CRITICAL):
            logging.critical(self.__str__())
    @staticmethod
    def build_error(dict):
        code=dict['code']
        e=get_error_by_code(code)
        e.note=dict['note']
        return e
    
#THREAD_NOT_EXIST=xmw.Error(code=60001,msg="Thread Not Exist!",level=xmw.ERROR_WARNING)

class OK(Error):
    code=0
    msg="OK!"
    level=logging.INFO
    name="OK"

class THREAD_NOT_EXIST(Error):
    code=60001
    msg="Thread Not Exist!"
    level=logging.WARNING
    name="THREAD_NOT_EXIST"

        
class ARGUMENT_FAULT(Error):
    code=60002
    msg="Argument Fault!"
    level=logging.ERROR
    name    ="ARGUMENT_FAULT"

class THREAD_EXIST(Error):
    code=60003
    msg="Thread already Exist!"
    level=logging.WARNING
    name="THREAD_EXIST"

class THREAD_PAGE_REFRESH(Error):
    code=60004
    msg="Refresh Thread Page List!"
    level=logging.INFO
    name="THREAD_PAGE_REFRESH"

class THREAD_ADDED(Error):
    code=60005
    msg="Thread added!"
    level=logging.INFO
    name="THREAD_ADDED"

class PAGE_EXIST(Error):
    code=60006
    msg="Page Exist!"
    level=logging.INFO
    name="PAGE_EXIST"

class NO_THREAD_REQUEST(Error):
    code=60007
    msg="There is no request to scan thread!"
    level=logging.INFO
    name="NO_THREAD_REQUEST"

class NO_POST_UPDATE_REQUEST(Error):
    code=60008
    msg="There is no request to update thread post!"
    level=logging.INFO
    name="NO_POST_UPDATE_REQUEST"  

class NEW_SCANPOST_Task(Error):
    code=60009
    msg="new task generated to scan post"
    level=logging.INFO
    name="NEW_SCANPOST_Task"  

class NO_SCANPOST_TASK(Error):
    code=60010
    msg="NO_SCANPOST_TASK wait to handle"
    level=logging.INFO
    name="NO_SCANPOST_TASK"  

class NOT_SUPPORTED_THREAD(Error):
    code=60011
    msg="This kind of thread not supported yet!"
    level=logging.INFO
    name="NOT_SUPPORTED_THREAD"      

class MISSION_ACCOMPLISHED(Error):
    code=60012
    msg="Finished scanning post for the thread!"
    level=logging.INFO
    name="MISSION_ACCOMPLISHED" 

class SCAN_ONE_PAGE(Error):
    code=60013
    msg="scanning one page post for the thread!"
    level=logging.INFO
    name="SCAN_ONE_PAGE" 

class UNSUPPORTED_MSG(Error):
    code=60014
    msg="received message unsupported!"
    level=logging.ERROR
    name="UNSUPPORTED_MSG" 

class POST_MSG_PARAM_FAULT(Error):
    code=60015
    msg="expected param not found while to build msg from post!"
    level=logging.ERROR
    name="POST_MSG_PARAM_FAULT"     

class NO_MSG_IN_QUEUE(Error):
    code=60016
    msg="there is no message to be handled!"
    level=logging.INFO
    name="NO_MSG_IN_QUEUE"     
    
class REQUESTED_PAGE_NOT_EXIST(Error):
    code=60017
    msg="REQUESTED_PAGE_NOT_EXIST!"
    level=logging.INFO
    name="REQUESTED_PAGE_NOT_EXIST" 

class NO_THREAD_TO_TRACE(Error):
    code=60018
    msg="there is no thread in bot for tracing!"
    level=logging.WARNING
    name="NO_THREAD_TO_TRACE"

class POST_NOT_EXIST(Error):
    code=60019
    msg="post not exist"
    level=logging.WARNING
    name="POST_NOT_EXIST"
                 
class OFFSET_OUT_OF_RANGE(Error):
    code=60020
    msg="required offset not scaned or not exist"
    level=logging.ERROR
    name="OFFSET_OUT_OF_RANGE"

class INVALIDE_THREAD_URL(Error):
    code=60021
    msg="could make thread from given url"
    level=logging.ERROR
    name="INVALIDE_THREAD_URL"  
      
class URL_ACCEPTED(Error):
    code=60022
    msg="bot will try to trace the given url "
    level=logging.INFO
    name="URL_ACCEPTED" 
    
class URL_BAD(Error):
    """
    when bot find that url is not a valid post page
    """
    code=60023
    msg="URL invalid  "
    level=logging.INFO
    name="URL_BAD"     
err_dict={
          60023:URL_BAD,
          60022:URL_ACCEPTED,
          60021:INVALIDE_THREAD_URL,
          60020:OFFSET_OUT_OF_RANGE,
          60019:POST_NOT_EXIST,
          60018:NO_THREAD_TO_TRACE,
          60017:REQUESTED_PAGE_NOT_EXIST,
          60016:NO_MSG_IN_QUEUE,
          60015:POST_MSG_PARAM_FAULT,
          60014:UNSUPPORTED_MSG,
          60013:SCAN_ONE_PAGE,
          60012:MISSION_ACCOMPLISHED,
          60011:NOT_SUPPORTED_THREAD,
          60010:NO_SCANPOST_TASK,
          60009:NEW_SCANPOST_Task,
          60008:NO_POST_UPDATE_REQUEST,
          60007:NO_THREAD_REQUEST,
          60006:PAGE_EXIST,
          60005:THREAD_ADDED,
          60004:THREAD_PAGE_REFRESH,
          60003:THREAD_EXIST,
          60002:ARGUMENT_FAULT,
          60001:THREAD_NOT_EXIST,
          0:OK,                
          }
def get_error_by_code(code):
    if type(code) is str:
        code=int(code)
    e=err_dict[code]
    return e

def main():
    e=get_error_by_code('60020')
    print e
    print e()
if __name__ == "__main__":
    main()